#This script is just a list of utilities for the {{ project_name }} project
# -*- coding: latin-1 -*-
from {{ module.lower() }} import {{ module }}
from {{ module.lower() }}.exceptions import *
from config_model import AnalysisProfile, Config
import os, sys

__all__ = ['validate_directory', 'validate_data_directory', 'validate_results_directory', 'validate_html_directory', 'validate_config', 'validate_database_running', 'validate_user', 'validate_password']


def validate_directory(_dir, strict=False, error_message=None):
	"""simply checks if the given directory actually exists"""
	if os.path.exists(_dir):
		return True
	else:
		if not strict:
			return False
		else:
			if error_message:
				raise error_message
			else:
				raise Exception, "Directory, '%s', does not exist!" % _dir

def validate_data_directory():
	"""simply checks if the data directory actually exists"""
	validate_directory({{ module }}.get_data_directory(), strict=True, error_message="Data Directory, '%s', does not exist!" % {{ module }}.get_data_directory())

def validate_results_directory():
	"""simply checks if the results directory actually exists"""
	validate_directory({{ module }}.get_results_directory(), strict=True, error_message="Results Directory, '%s', does not exist!" % {{ module }}.get_results_directory())

def validate_html_directory():
	"""simply checks if the html directory actually exists"""
	validate_directory({{ module }}.get_html_directory(), strict=True, error_message="HTML Directory, '%s', does not exist!" % {{ module }}.get_html_directory())

def validate_config(config_name):
	"""checks if the configuration has been created"""
	session = {{ module }}.get_config_session()

	_key = session.query(Config).filter(Config.name == config_name).limit(1).all()
	if not _key:
		raise Exception, "Configuration, '%s', does not exist. Please add a new configuration via '(meta)make config:new'."
	else:
		return True

def validate_database_running():
	"""determines if the database is running"""
	from {{ module.lower() }}.utilities import new_session
	try:
		raise NotImplementedError
		# session = new_session({{ module }}.login(root_user))
		# session.close()
	except Exception, e:
		raise e

def validate_user(user):
	"""This checks to see if a user exists"""

	if not {{ module }}.login_info().has_key(user):
		print "\n There is not a listed user by this name: "+str(user)
		print " Please add '"+str(user)+"' and the proper credentials to /config/login_info.csv"
		raise UserNotFound
	return True

def validate_password(user):
	"""This prompts user for credentials if root and then returns if the input correctly matched"""
	from {{ module.lower() }}.validations import validate_user

	#does the user exist?
	validate_user(user)

	#crypted password
	crypted = {{ module }}.login_info()[user]['password']

	#if no crypted password (i.e. root), then prompt
	if(crypted == ''):
		return crypto.hidden_prompt("\n Please enter a password for '"+user+"'@'"+self.current_host+"': ")
	#otherwise decode the password
	else:
		return crypto.decode(base64.b64decode(crypted))

